package com.cyy.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyy.base.exception.CommonError;
import com.cyy.base.exception.LearningException;
import com.cyy.content.mapper.TeachplanMapper;
import com.cyy.content.mapper.TeachplanMediaMapper;
import com.cyy.content.model.dto.BindTeachplanMediaDto;
import com.cyy.content.model.dto.SaveTeachplanDto;
import com.cyy.content.model.dto.TeachPlanDto;
import com.cyy.content.model.po.Teachplan;
import com.cyy.content.model.po.TeachplanMedia;
import com.cyy.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 课程计划 服务实现类
 * </p>
 *
 * @author cyy
 */
@Slf4j
@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements TeachplanService {

    @Autowired
    TeachplanMapper teachplanMapper;

    @Autowired
    TeachplanMediaMapper teachplanMediaMapper;

    @Override
    public List<TeachPlanDto> findTeachPlanTree(Long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }

    @Override
    public void saveTeachPlan(SaveTeachplanDto teachPlanDto) {
        Long id = teachPlanDto.getId();
        if (id != null) {//修改计划
            Teachplan teachplan = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(teachPlanDto, teachplan);
            teachplanMapper.updateById(teachplan);
        } else {
            //取出同父同级别的课程计划数量
            int count = getTeachPlanCount(teachPlanDto.getCourseId(), teachPlanDto.getParentid());
            Teachplan teachplan = new Teachplan();
            teachplan.setOrderby(count + 1);
            BeanUtils.copyProperties(teachPlanDto, teachplan);
            teachplanMapper.insert(teachplan);
        }
    }

    @Override
    public void deleteTeachPlan(Long teachplanId) {
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(teachplanId != null, Teachplan::getParentid, teachplanId);
        Integer count = teachplanMapper.selectCount(wrapper);
        if (count != 0) {
            LearningException.cast("课程计划信息还有子级信息，无法操作");
        } else {
            //删除媒体信息
            LambdaQueryWrapper<TeachplanMedia> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(teachplanId != null, TeachplanMedia::getTeachplanId, teachplanId);
            teachplanMediaMapper.delete(queryWrapper);
            teachplanMapper.delete(wrapper);
        }
    }

    @Override
    public void moveUpTeachPlan(Long teachplanId) {
       /* 先找一个上边有课程计划的进行测试，向上移动后两个交换顺序。
        再找最上边的课程计划向上移动，操作后位置不变因为已经在最上边了。*/
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if (teachplan == null) {
            LearningException.cast(CommonError.OBJECT_NULL);
        }
//        LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq()
    }

    @Override
    public void moveDownTeachPlan(Long teachplanId) {

    }

    /**
     * 同父同级别的课程计划数量
     *
     * @param courseId
     * @param parentId
     * @return
     */
    private int getTeachPlanCount(Long courseId, Long parentId) {
        LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teachplan::getCourseId, courseId);
        wrapper.eq(Teachplan::getParentid, parentId);
        Integer count = teachplanMapper.selectCount(wrapper);
        return count;
    }

    @Override
    public void associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if(teachplan == null){
            LearningException.cast("教学计划不存在");
        }
        Integer grade = teachplan.getGrade();
        if(grade != 2){
            LearningException.cast("只允许第二级教学计划绑定媒资文件");
        }
        //课程id
        Long courseId = teachplan.getCourseId();
        String fileName = bindTeachplanMediaDto.getFileName();

        //先删除原来的教学计划绑定的媒资
        teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId, teachplanId));
        //在添加教学计划与媒资的绑定关系
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        BeanUtils.copyProperties(bindTeachplanMediaDto, teachplanMedia);
        teachplanMedia.setCourseId(courseId);
        teachplanMedia.setMediaFilename(fileName);
        teachplanMedia.setCreateDate(LocalDateTime.now());
        teachplanMediaMapper.insert(teachplanMedia);
    }

}
